package com.xr.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xr.constant.MessageConstant;
import com.xr.exception.CustomException;
import com.xr.mapper.CheckGroupMapper;
import com.xr.pojo.CheckGroup;
import com.xr.query.QueryPageBean;
import com.xr.result.PageResult;
import com.xr.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),true);
        Page<CheckGroup> page = checkGroupMapper.selectByFindString(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Integer DelCheckGroup(Integer id) throws CustomException {
        //判断检查组是否被套餐使用了
        Integer count = checkGroupMapper.isUseInSetMeal(id);
        if(count>0){
            //被使用了则不能删除,手动抛出异常
            throw new CustomException(MessageConstant.CHECKGROUP_IN_USE);
        }else{
            //没被使用
            //先解散，删除检查组与检查项的关系
            checkGroupMapper.delCheckGroupCheckItemByGroupId(id);
            //再来删除检查组
            return checkGroupMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupMapper.selectByExample(null);
    }

    @Override
    public void AddCheckGroup(CheckGroup checkGroup, Integer [] checkitemIds) {
        checkGroupMapper.addCheckGroup(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        this.setCheckGroupAndCheckItem(checkGroupId,checkitemIds);
    }

    @Override
    public void UpdCheckGroup(CheckGroup checkGroup, Integer [] checkitemIds) {
        checkGroupMapper.updateByPrimaryKeySelective(checkGroup);
        checkGroupMapper.deleteRelation(checkGroup.getId());
        Integer checkGroupId = checkGroup.getId();
        this.setCheckGroupAndCheckItem(checkGroupId,checkitemIds);

    }

    @Override
    public List<Integer> selectItemsByGroupId(Integer groupId) {
        return checkGroupMapper.selectItemsByGroupId(groupId);
    }

    public void setCheckGroupAndCheckItem(Integer checkGroupId, Integer [] checkitemIds){
        if(checkitemIds != null && checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds){
                Map<String,Integer> map = new HashMap<>();
                map.put("checkGroupId",checkGroupId);
                map.put("checkitemId",checkitemId);
                checkGroupMapper.setCheckGroupAndCheckItem(map);
            }
        }
    }

}
